package com.jinfang.bankpractice.controller;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jinfang.bankpractice.dto.ReturnData;
import com.jinfang.bankpractice.model.User;
import com.jinfang.bankpractice.service.QuesbankService;
import com.jinfang.bankpractice.service.RecordService;
import com.wordnik.swagger.annotations.ApiOperation;

@RestController
public class QuestionController {

	 private Logger logger = LoggerFactory.getLogger(QuestionController.class);

	    @Autowired
	    private QuesbankService quesbankService;
	    
	    @Autowired
	    private RecordService recordService;
	    
	    /**
	     * 
	     * @param amount
	     * @param area
	     * @return
	     */
	    @ApiOperation(value="获取模拟测试的题目",notes="requires noting")
	    @RequestMapping(value="/api/test/questions",method=RequestMethod.GET)
	    public Object select(@RequestParam("sessionid")String sessionid,
	    		@RequestParam("amount")Integer amount,
	    		@RequestParam("course")int course,HttpSession session) {
	    	User user=(User) session.getAttribute(sessionid);	
	    	if(user==null){
	    		return new ReturnData(ReturnData.UNAUTHORIZED, "请重新登录", "用户未找到", null);
	    	}
	    	logger.info("get userid from session,userid={}",user.getId());
	    	return quesbankService.test(amount, course);
	    }
	    
	    @ApiOperation(value="获取模拟练习的题目",notes="requires noting")
	    @RequestMapping(value="/api/practice/questions/",method=RequestMethod.GET)
	    public Object practice(
	    		@RequestParam("sessionid")String sessionid,
	    		@RequestParam("amount")Integer amount,
	    		@RequestParam("course")int course,HttpSession session) {
	    	User user=(User) session.getAttribute(sessionid);	
	    	if(user==null){
	    		return new ReturnData(ReturnData.UNAUTHORIZED, "请重新登录", "用户未找到", null);
	    	}
	    	logger.info("get userid from session,userid={}",user.getId());
	    	return quesbankService.practice(amount, course, user.getId());
	    }

	  
	    
		@ApiOperation(value="获取错题的题目",notes="requires noting")
	    @RequestMapping(value="/api/practice/errors",method=RequestMethod.GET)
	    public Object error(
	    		@RequestParam("sessionid")String sessionid,
	    		@RequestParam("amount")Integer amount,
	    		@RequestParam("course")int course,HttpSession session) {
	    	
			User user=(User) session.getAttribute(sessionid);	
	    	if(user==null){
	    		return new ReturnData(ReturnData.UNAUTHORIZED, "请重新登录", "用户未找到", null);
	    	}
	    	logger.info("get userid from session,userid={}",user.getId());
	    	return quesbankService.error(amount, course, user.getId());
	    }
	    
	    /**
	    * 提交
	     * @param sessionid
	     * @param amount
	     * @param course
	     * @param session
	     * @return
	     */
		@ApiOperation(value="模拟测试和模拟练习的提交",notes="requires noting")
	    @RequestMapping(value={"/api/test/","/api/practice"},method=RequestMethod.POST)
	    public ReturnData testSubmit(
	    		@RequestParam("sessionid")String sessionid,
	    		@RequestParam("score")int score,
	    		@RequestParam("doneids")String doneids,
	    		@RequestParam("errorids")String errorids,HttpSession session,
	    		@RequestParam("course")int course) {
			User user=(User) session.getAttribute(sessionid);	
	    	if(user==null){
	    		return new ReturnData(ReturnData.UNAUTHORIZED, "请重新登录", "用户未找到", null);
	    	}
	    	logger.info("get userid from session,userid={}",user.getId());    	
	    	boolean yes=recordService.addorUpdate(score, doneids, errorids, user.getId(), course);
	    	return ReturnData.buildSuccess("提交成功", yes);
	    }
	    
	    /**
	     * 错题训练提交
	     * @param sessionid
	     * @param rightids
	     * @param session
	     * @param course
	     * @return
	     */
		@ApiOperation(value="错题训练提交",notes="requires noting")
	    @RequestMapping(value={"/api/practice/errors"},method=RequestMethod.POST)
	    public Object errorSubmit(@RequestParam("sessionid")String sessionid,
	    		@RequestParam("rightids")String rightids,HttpSession session,
	    		@RequestParam("course")int course) {
			User user=(User) session.getAttribute(sessionid);	
	    	if(user==null){
	    		return new ReturnData(ReturnData.UNAUTHORIZED, "请重新登录", "用户未找到", null);
	    	}
	    	logger.info("get userid from session,userid={}",user.getId());    	
	    	boolean yes=recordService.reduceError(rightids,  user.getId(), course,user.getId());
	    	return ReturnData.buildSuccess("提交成功", yes);
	    } 
	    
	    
}
